using System;
using PEMath;
using UnityEngine;

/****************************************************
// 功能：逻辑定时器
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/4/8 16:25:32
*****************************************************/

public class LogicTimer {
    public bool IsActive { set; get; }

    private PEInt delayTime;
    private readonly PEInt loopTime;

    private readonly PEInt delta;
    private PEInt callbackCount;
    private Action cb;

    public LogicTimer(Action cb, PEInt delayTime, int loopTime = 0) {
        this.cb = cb;
        this.delayTime = delayTime;
        this.loopTime = loopTime;
        delta = Constants.SERVER_LOGIC_FRAME_ID;
        IsActive = true;
    }

    public void TickTimer() {
        callbackCount += delta;
        if(callbackCount >= delayTime && cb != null) {
            cb();
            if(loopTime == 0) {
                IsActive = false;
                cb = null;
            }
            else {
                callbackCount -= delayTime;
                delayTime = loopTime;
            }
        }
    }
}